package com.example.dell.wisdom_farm;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;
import com.example.dell.wisdom_farm.pojo.model;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;



public class view2 extends Fragment
{
    private GridView gridView;
    private MyAdapter myAdapter;
    private ArrayList<model> list=new ArrayList<>();

    private  String[] titles=new String[]{"运行时间:","运行时间：","运行时间:","运行时间:"};
    private int[] image={R.mipmap.baojingqi,R.mipmap.choushuibeng,R.mipmap.tongfengji,R.mipmap.zhaomingdeng};

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View view=layoutInflater.inflate(R.layout.view2,gridView,false);
        gridView=(GridView)view.findViewById(R.id.view2_gridview_1) ;
        list.clear();
        list.add(new model("通风机","开启",""));
        list.add(new model("照明灯","关闭",""));
        list.add(new model("抽水泵","关闭",""));
        list.add(new model("报警器","开启",""));
        myAdapter=new MyAdapter();
        gridView.setAdapter(myAdapter);
        return view;
    }

    public class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return list.size();
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return 0;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.view2_item_view, null);
            final TextView textView1 = (TextView) view.findViewById(R.id.view3_textview_1);
            final TextView textView2 = (TextView) view.findViewById(R.id.view3_textview_2);
            ImageView imageView = (ImageView) view.findViewById(R.id.view_imageview_1);
            ToggleButton toggleButton = (ToggleButton) view.findViewById(R.id.view3_toggolebtn_1);
            final String name = list.get(position).getImage_name();
            final String state = list.get(position).getImage_state();
            String title = list.get(position).getImage_title();
            Timer timer;
            if (state.equals("开启")) {

                toggleButton.setChecked(true);
                textView1.setText(name + ":开启");
                textView2.setText(title + titles[position]);
            } else {
                toggleButton.setChecked(false);
                textView1.setText(name + ":关闭");
            }
            toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        textView1.setText(name + ":开启");
                        textView2.setText(titles[position]);
                    } else {
                        textView1.setText(name + ":关闭");
                        textView2.setText("");
                    }
                }
            });
            imageView.setImageResource(image[position]);


            return view;

        }
    }




}
